package com.zhu.shopcenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zhu.shopcenter.mapper.TradeOrderMapper;
import com.zhu.shopcenter.pojo.TradeOrder;
import com.zhu.shopcenter.pojo.TradePayLog;
import com.zhu.shopcenter.mapper.TradePayLogMapper;
import com.zhu.shopcenter.service.ITradePayLogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhu.shopcenter.vo.PayLogVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;

/**
 * <p>
 * 支付日志表 服务实现类
 * </p>
 *
 * @author zhu
 * @since 2022-08-19
 */
@Service
public class TradePayLogServiceImpl extends ServiceImpl<TradePayLogMapper, TradePayLog> implements ITradePayLogService {
     @Autowired
     private TradeOrderMapper orderMapper;
     @Autowired
     private TradePayLogMapper payLogMapper;
    @Override
    public Boolean addPayHistory(String id, PayLogVo payLogVo) {
        /**
         * 1.更改支付状态为1
         */
        String transactionId = payLogVo.getTransactionId();//交易流水账号
        if(StringUtils.isEmpty(transactionId)){
            return false;
        }
        String orderNo = payLogVo.getOrderNo();//本机生成的订单账号
        LambdaQueryWrapper<TradeOrder> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(TradeOrder::getOrderNo,orderNo);
        TradeOrder order = orderMapper.selectOne(queryWrapper);
        order.setStatus(1);
        orderMapper.updateById(order);
        /**
         * 添加支付历史记录
         */
        int insert=0;
        try {
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String totalFee = payLogVo.getTotalFee();
            BigDecimal bigDecimal = new BigDecimal(totalFee);
            TradePayLog tradePayLog=new TradePayLog();
        tradePayLog.setId(id);
        tradePayLog.setAttr(order.getCourseTitle()); //购买信息
        tradePayLog.setPayType(2);
        tradePayLog.setOrderNo(payLogVo.getOrderNo());
        tradePayLog.setTotalFee(bigDecimal);
        tradePayLog.setTransactionId(payLogVo.getTransactionId());
        tradePayLog.setPayTime(simpleDateFormat.parse(payLogVo.getPayTime()) );
        insert = payLogMapper.insert(tradePayLog);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        if(insert==1){
            return true;
        }
        return false;
    }
}
